// 四舍五入保留两位
exports.keepTwoDecimal = (num) => {
	var result = parseFloat(num)
	if (isNaN(result)) {
		return false
	}
	result = Math.round(num * 100) / 100
	return result
};

// 截取指定字符串前后的内容，0前面，1后面
exports.getCaption = (obj, state) => {
	var index = obj.lastIndexOf("\.");
	if (state == 0) {
		obj = obj.substring(0, index);
	} else {
		obj = obj.substring(index + 1, obj.length);
	}
	return obj;
}

// 取出两个数组中不同的项
exports.getDiffer = (arr1, arr2) => {
	return arr1.concat(arr2).filter(function(value, index, arr) {
		return arr.indexOf(value) === arr.lastIndexOf(value);
	});
}

// 去除数组里重复的值
exports.unique = (arr) => {
	for (let i = 0; i < arr.length; i++) {
		for (let j = i + 1; j < arr.length; j++) {
			if (arr[i] == arr[j]) {
				arr.splice(j, 1);
				j--;
			}
		}
	}
	return arr;
}

// 对象数组根据属性分组 arrayGroupBy(data,'name')
exports.arrayGroupBy = (list, groupId) => {
	function groupBy(array, f) {
		let groups = {};
		array.forEach(function(o) {
			var group = JSON.stringify(f(o));
			groups[group] = groups[group] || [];
			groups[group].push(o);
		});
		return Object.keys(groups).map(function(group) {
			return groups[group];
		});
	};
	let sorted = groupBy(list, function(item) {
		return [item[groupId]];
	});
	return sorted;
};

// 深拷贝数组
exports.copyLinearArray = (source) => {
	var sourceCopy = [],
		objectCopy = {};
	if(source.constructor === Object){
		//如果是对象
		Object.keys(source).forEach((item)=>{
			if (source[item] && source[item] != 0) {
				if(source[item].constructor === Object || Array.isArray(source[item])){
					objectCopy[item] = this.copyLinearArray(source[item])
				}else{
					objectCopy[item] = source[item]
				}
			}else{
				objectCopy[item] = source[item]
			}
		})
    return objectCopy
	} else if(Array.isArray(source)){
	//如果是数组
		source.forEach((item,index)=>{
			if(source[index].constructor === Object || Array.isArray(source[index])){
				sourceCopy[index] = this.copyLinearArray(source[index])
			}else{
				sourceCopy[index] = source[index];
			}
		})
		return sourceCopy;
	}
}